# -*- coding: utf-8 -*-

import pytest

from appium import webdriver
from appium.options.android import UiAutomator2Options
from appium.webdriver.appium_service import AppiumService
from appium.webdriver.common.appiumby import AppiumBy

# 开启服务端
APPIUM_HOST = '127.0.0.1'
APPIUM_PORT = 4723
@pytest.fixture(scope="session")
def start_appium_service():
    server = AppiumService()
    server.start(args=['--address', APPIUM_HOST, '-p', str(APPIUM_PORT)], timeout_ms=60000)
    yield server
    server.stop()

# 创建客户端到服务端的会话
def create_appium_session_by_api(custom_opts = None):
    options = UiAutomator2Options()
    if custom_opts is not None:
        options.load_capabilities(custom_opts)
    return webdriver.Remote(f'http://{APPIUM_HOST}:{APPIUM_PORT}', options=options)

def test_element_attribute(start_appium_service):
    custom_opts = {
        "appium:appPackage": "com.google.android.dialer",
        "appium:appActivity": ".extensions.GoogleDialtactsActivity",
        "appium:avd": "testPhone",
    }
    driver = create_appium_session_by_api(custom_opts)

    # 获取联系人页面
    contact_id_full = "com.google.android.dialer:id/tab_contacts"
    contact = driver.find_element(AppiumBy.ID, value=contact_id_full)

    print("\n=======print element attribute==========")
    print(f"======= checkable: {contact.get_attribute('checkable')}")
    print(f"======= checked: {contact.get_attribute('checked')}")
    print(f"======= class/className: {contact.get_attribute('className')}")
    print(f"======= clickable: {contact.get_attribute('clickable')}")
    print(f"======= content-desc/contentDescription: {contact.get_attribute('content-desc')}")
    print(f"======= enabled: {contact.get_attribute('enabled')}")
    print(f"======= focusable: {contact.get_attribute('focusable')}")
    print(f"======= focused: {contact.get_attribute('focused')}")
    print(f"======= long-clickable/longClickable: {contact.get_attribute('long-clickable')}")
    print(f"======= package: {contact.get_attribute('package')}")
    print(f"======= password: {contact.get_attribute('password')}")
    print(f"======= resource-id/resourceId: {contact.get_attribute('resource-id')}")
    print(f"======= scrollable: {contact.get_attribute('scrollable')}")
    print(f"======= selection-start: {contact.get_attribute('selection-start')}")
    print(f"======= selection-end: {contact.get_attribute('selection-end')}")
    print(f"======= selected: {contact.get_attribute('selected')}")
    print(f"======= text/name: {contact.get_attribute('name')}")
    print(f"======= hint: {contact.get_attribute('hint')}")
    print(f"======= bounds: {contact.get_attribute('bounds')}")
    print(f"======= displayed: {contact.get_attribute('displayed')}")
    print(f"======= contentSize: {contact.get_attribute('contentSize')}")
    print(f"======= extras: {contact.get_attribute('extras')}")